package cn.bluethink.eguan.core.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluethink.eguan.core.entity.EgAcountEntity;
import cn.bluethink.eguan.core.mapper.EgAcountMapper;
import cn.bluethink.eguan.model.core.EgAcount;
import cn.bluethink.eguan.utils.IdMakerUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * 账户业务层
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.1.0
 * @note 修订历史： 1.wanglei于2018/10/13设计并构建初始版本v0.1.0
 */
@Service
public class EgAcountService {

    @Autowired
    private EgAcountMapper acountMapper;

    private IdMakerUtils idMakerUtil = new IdMakerUtils();

    /**
     * 创建账户
     * 
     * @param name
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public EgAcount create(String name) throws Exception {
        // 自动生成账户ID
        EgAcountEntity entity = new EgAcountEntity(idMakerUtil.nextId(), name, 0);
        int insertCount = acountMapper.insertSelective(entity);
        return insertCount > 0 ? entity.createEgAcount() : null;
    }

    /**
     * 根据主键删除账户信息
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public Boolean del(Long id) throws Exception {
        int deletCount = acountMapper.deleteByPrimaryKey(id);
        return deletCount > 0 ? true : false;
    }

    /**
     * 更新账户信息(利用更新时间的时间戳进行乐观锁操作)
     * 
     * @param acount
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public EgAcount update(EgAcount acount) throws Exception {
        // ID不能为空
        if (acount.getId() == null || acount.getId() == 0) {
            return null;
        }
        EgAcountEntity entity = new EgAcountEntity(acount);
        // 构造条件，注意账户的更新时间
        Example example = new Example(EgAcountEntity.class);
        example.and().andEqualTo("id", acount.getId()).andEqualTo("utime", acount.getUtime());
        int updateCount = acountMapper.updateByExampleSelective(entity, example);
        return updateCount > 0 ? entity.createEgAcount() : null;
    }

    /**
     * 根据账户ID查询账户信息(拿到更新时间)
     * 
     * @param id
     * @return
     * @throws Exception
     */
    public EgAcount query(Long id) throws Exception {
        EgAcountEntity entity = acountMapper.selectByPrimaryKey(id);
        return entity != null ? entity.createEgAcount() : null;
    }

    /**
     * 更新账户的信息。 bAddBt为ture表示账户增加金币，否则账户扣除金币
     * 
     * @param acountId
     * @param bt
     * @param bAddBt 
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public Boolean updateAcount(Long acountId, Integer bt, Boolean bAddBt) throws Exception {
        // 查询账户信息
        EgAcount acount = query(acountId);
        if (acount == null) {
            return false;
        }
        if (bAddBt) {
            acount.setBt(acount.getBt() + bt); // 账户增加相应金币
        } else {
            if (acount.getBt() < bt) {
                throw new RuntimeException("账户资金不够！");
            }
            acount.setBt(acount.getBt() - bt); // 扣除账户相应金币
        }
        // 更新账户信息
        if (update(acount) == null) {
            throw new RuntimeException("账户更新失败！");
        }
        return true;
    }
}
